package NovoSISAM.Gui;

import javax.swing.JOptionPane;
import NovoSISAM.Model.Medico;
import static NovoSISAM.DataBase.DataBase.*;
import static NovoSISAM.Dao.MedicoDao.*;

/**
 *
 * @author Cleiviane
 */
public class Cadastro_Medicos extends javax.swing.JInternalFrame {

    int codigo = 0;
    Medico medico;

    /** Creates new form Cadastro_Medicos */
    public Cadastro_Medicos() {
        initComponents();
        ConectarDataBase();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtcodigo = new javax.swing.JTextField();
        cbexclusivo = new javax.swing.JCheckBox();
        cbotipocontrato = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        txtcrm = new javax.swing.JTextField();
        txtespecialidade = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txttelefone = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtnascimento = new javax.swing.JTextField();
        txtemail = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtcpf = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtendereco = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtnome = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        botaoNovo = new javax.swing.JButton();
        botaoSalvar = new javax.swing.JButton();
        botaoCancelar = new javax.swing.JButton();
        botaoExcluir = new javax.swing.JButton();
        botaoAlterar = new javax.swing.JButton();

        setClosable(true);
        setMaximizable(true);
        setTitle("Médicos - SISAM");

        txtcodigo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtcodigoFocusLost(evt);
            }
        });

        cbexclusivo.setText("Médico Exclusivo");

        cbotipocontrato.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Diarista", "Quinzenalista", "Mensalista", " " }));
        cbotipocontrato.setToolTipText("Diarista\nSemanal\nQuinzenal\nMensal\n");

        jLabel11.setText("Tipo de Contrato:");

        jLabel7.setText("Telefone:");

        jLabel6.setText("CRM:");

        jLabel2.setText("Cód. Id:");

        txttelefone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txttelefoneActionPerformed(evt);
            }
        });

        jLabel3.setText("Nome:");

        txtnascimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtnascimentoActionPerformed(evt);
            }
        });

        jLabel4.setText("Endereço:");

        jLabel5.setText("CPF:");

        jLabel9.setText("Especialidade:");

        jLabel8.setText("Email:");

        jLabel10.setText("D.Nascimento:");

        jLabel12.setFont(new java.awt.Font("Monotype Corsiva", 1, 24));
        jLabel12.setForeground(new java.awt.Color(51, 51, 255));
        jLabel12.setText("Médicos");

        botaoNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NovoSISAM/Gui/Imagens/novo.jpg"))); // NOI18N
        botaoNovo.setText("Novo");
        botaoNovo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoNovo.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoNovo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoNovoActionPerformed(evt);
            }
        });

        botaoSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NovoSISAM/Gui/Imagens/salvar.jpg"))); // NOI18N
        botaoSalvar.setText("Salvar");
        botaoSalvar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoSalvar.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoSalvar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSalvarActionPerformed(evt);
            }
        });

        botaoCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NovoSISAM/Gui/Imagens/cancelar.jpg"))); // NOI18N
        botaoCancelar.setText("Cancelar");
        botaoCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoCancelar.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCancelarActionPerformed(evt);
            }
        });

        botaoExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NovoSISAM/Gui/Imagens/excluir.jpg"))); // NOI18N
        botaoExcluir.setText("Excluir");
        botaoExcluir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoExcluir.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoExcluir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoExcluirActionPerformed(evt);
            }
        });

        botaoAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NovoSISAM/Gui/Imagens/alterar.jpg"))); // NOI18N
        botaoAlterar.setText("Alterar");
        botaoAlterar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoAlterar.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoAlterar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoAlterarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addGap(4, 4, 4)
                        .addComponent(cbotipocontrato, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(155, 155, 155)
                        .addComponent(cbexclusivo))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(47, 47, 47)
                                .addComponent(jLabel2))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(47, 47, 47)
                                .addComponent(jLabel3))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(38, 38, 38)
                                .addComponent(jLabel4))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(64, 64, 64)
                                .addComponent(jLabel5))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(41, 41, 41)
                                .addComponent(jLabel7))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(59, 59, 59)
                                .addComponent(jLabel8))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addComponent(jLabel9)))
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtespecialidade, javax.swing.GroupLayout.DEFAULT_SIZE, 544, Short.MAX_VALUE)
                            .addComponent(txtemail, javax.swing.GroupLayout.DEFAULT_SIZE, 544, Short.MAX_VALUE)
                            .addComponent(txtendereco, javax.swing.GroupLayout.DEFAULT_SIZE, 544, Short.MAX_VALUE)
                            .addComponent(txtnome, javax.swing.GroupLayout.DEFAULT_SIZE, 544, Short.MAX_VALUE)
                            .addComponent(txtcodigo, javax.swing.GroupLayout.DEFAULT_SIZE, 544, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtcpf, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                                    .addComponent(txttelefone, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel6)
                                        .addGap(27, 27, 27))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel10)
                                        .addGap(18, 18, 18)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txtnascimento)
                                    .addComponent(txtcrm, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addGap(20, 20, 20))
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(476, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(111, Short.MAX_VALUE)
                .addComponent(botaoSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(botaoNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(botaoExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botaoAlterar)
                .addGap(18, 18, 18)
                .addComponent(botaoCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botaoAlterar, botaoCancelar, botaoExcluir, botaoNovo, botaoSalvar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel2))
                    .addComponent(txtcodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(txtnome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(txtendereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel5))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtcpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtcrm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6)))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txttelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtnascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel8))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtemail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jLabel9))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(txtespecialidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jLabel11))
                    .addComponent(cbotipocontrato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbexclusivo))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botaoCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                    .addComponent(botaoAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botaoExcluir, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                    .addComponent(botaoNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botaoSalvar))
                .addGap(46, 46, 46))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {botaoAlterar, botaoCancelar, botaoExcluir, botaoNovo, botaoSalvar});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txttelefoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txttelefoneActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txttelefoneActionPerformed

    private void txtnascimentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtnascimentoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtnascimentoActionPerformed

    private void botaoNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoNovoActionPerformed
        limpacampos();
        destravacampos();
        txtcodigo.setEditable(true);
        txtcodigo.requestFocus();
        botaoAlterar.setEnabled(false);
        botaoCancelar.setEnabled(true);
        botaoExcluir.setEnabled(false);
        botaoNovo.setEnabled(false);
        botaoSalvar.setEnabled(true);
}//GEN-LAST:event_botaoNovoActionPerformed

    private void botaoSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSalvarActionPerformed
        medico = new Medico();

        medico.setCod_medico(Integer.parseInt(txtcodigo.getText()));
        medico.setNome(txtnome.getText());
        medico.setEndereco(txtendereco.getText());
        medico.setCpf(txtcpf.getText());
        medico.setCrm(txtcrm.getText());
        medico.setTelefone(txttelefone.getText());
        medico.setData_nasc(txtnascimento.getText());
        medico.setEmail(txtemail.getText());
        medico.setEspecialidade(txtespecialidade.getText());
        medico.setTipo_contrato(cbotipocontrato.getSelectedIndex());
        if (cbexclusivo.isSelected()) {
            medico.setExclusivo(1);
        } else {
            medico.setExclusivo(0);
        }

        if (txtcodigo.isEditable()) {
            if (inserirMedico(medico)) {
                JOptionPane.showMessageDialog(null, "Medico Cadastrado com Sucesso");
            } else {
                JOptionPane.showMessageDialog(null, "Nao foi possivel Salvar");
            }

            limpacampos();
            destravacampos();
            botaoAlterar.setEnabled(false);
            botaoCancelar.setEnabled(false);
            botaoExcluir.setEnabled(false);
            botaoNovo.setEnabled(true);
            botaoSalvar.setEnabled(true);

        } else {
            AtualizarMedico(medico);
            travacampos();
            botaoAlterar.setEnabled(true);
            botaoCancelar.setEnabled(true);
            botaoExcluir.setEnabled(true);
            botaoNovo.setEnabled(true);
            botaoSalvar.setEnabled(false);
        }
}//GEN-LAST:event_botaoSalvarActionPerformed

    private void botaoCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCancelarActionPerformed
        if (txtcodigo.getText().isEmpty()) {
                botaoAlterar.setEnabled(false);
                botaoCancelar.setEnabled(false);
                botaoExcluir.setEnabled(false);
                botaoNovo.setEnabled(true);
                botaoSalvar.setEnabled(false);
                destravacampos();
            } else {
                botaoAlterar.setEnabled(true);
                botaoCancelar.setEnabled(false);
                botaoExcluir.setEnabled(true);
                botaoNovo.setEnabled(true);
                botaoSalvar.setEnabled(false);
                travacampos();
            }
}//GEN-LAST:event_botaoCancelarActionPerformed

    private void botaoExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoExcluirActionPerformed
        if (medico == null) {
            JOptionPane.showMessageDialog(this, "Selecione alguem para excluir!");
            return;
        } else {
            boolean result = JOptionPane.showInternalConfirmDialog(
                    this, "Deseja realmente excluir esse cadastro?") == 0;

            if (result == true) {
                medico.setCod_medico(Integer.parseInt(txtcodigo.getText()));
                excluirMedico(medico);
                limpacampos();
                destravacampos();
                botaoAlterar.setEnabled(false);
                botaoCancelar.setEnabled(false);
                botaoExcluir.setEnabled(false);
                botaoNovo.setEnabled(true);
                botaoSalvar.setEnabled(true);
            } else {
                return;
            }
        }
}//GEN-LAST:event_botaoExcluirActionPerformed

    public void limpacampos() {
        txtcodigo.setText(null);
        txtnome.setText(null);
        txtcpf.setText(null);
        txtcrm.setText(null);
        txtemail.setText(null);
        txtendereco.setText(null);
        txtespecialidade.setText(null);
        txtnascimento.setText(null);
        txttelefone.setText(null);
        cbexclusivo.setSelected(false);
        cbotipocontrato.setSelectedIndex(0);
    }

    public void travacampos() {
        txtcodigo.setEditable(false);
        txtnome.setEditable(false);
        txtcpf.setEditable(false);
        txtcrm.setEditable(false);
        txtemail.setEditable(false);
        txtendereco.setEditable(false);
        txtespecialidade.setEditable(false);
        txtnascimento.setEditable(false);
        txttelefone.setEditable(false);
        cbexclusivo.setEnabled(false);
        cbotipocontrato.setEnabled(false);
    }

    public void destravacampos() {
        txtcodigo.setEditable(true);
        txtnome.setEditable(true);
        txtcpf.setEditable(true);
        txtcrm.setEditable(true);
        txtemail.setEditable(true);
        txtendereco.setEditable(true);
        txtespecialidade.setEditable(true);
        txtnascimento.setEditable(true);
        txttelefone.setEditable(true);
        cbexclusivo.setEnabled(true);
        cbotipocontrato.setEnabled(true);
    }

    private void botaoAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoAlterarActionPerformed
        this.destravacampos();
        txtcodigo.setEditable(false);
        botaoAlterar.setEnabled(false);
        botaoCancelar.setEnabled(true);
        botaoExcluir.setEnabled(true);
        botaoNovo.setEnabled(false);
        botaoSalvar.setEnabled(true);
}//GEN-LAST:event_botaoAlterarActionPerformed

    private void txtcodigoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtcodigoFocusLost
        int codMedico = Integer.parseInt(txtcodigo.getText());
            medico = localizarMedicoPorCodigo(codMedico);

            if (medico != null) {
                preencheCampos(medico);
            }
    }//GEN-LAST:event_txtcodigoFocusLost

    private void preencheCampos(Medico medico) {
        txtnome.setText(medico.getNome());
        txtcpf.setText(medico.getCpf());
        txtendereco.setText(medico.getEndereco());
        txtcrm.setText(medico.getCrm());
        txtnascimento.setText(medico.getData_nasc());
        txttelefone.setText(medico.getTelefone());
        txtemail.setText(medico.getEmail());
        txtespecialidade.setText(medico.getEspecialidade());
        cbotipocontrato.setSelectedIndex(medico.getTipo_contrato());
        if (medico.getExclusivo() == 1) {
            cbexclusivo.setSelected(true);
        } else {
            cbexclusivo.setSelected(false);
        }
        this.travacampos();
        botaoAlterar.setEnabled(true);
        botaoCancelar.setEnabled(false);
        botaoExcluir.setEnabled(true);
        botaoNovo.setEnabled(true);
        botaoSalvar.setEnabled(false);

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoAlterar;
    private javax.swing.JButton botaoCancelar;
    private javax.swing.JButton botaoExcluir;
    private javax.swing.JButton botaoNovo;
    private javax.swing.JButton botaoSalvar;
    private javax.swing.JCheckBox cbexclusivo;
    private javax.swing.JComboBox cbotipocontrato;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField txtcodigo;
    private javax.swing.JTextField txtcpf;
    private javax.swing.JTextField txtcrm;
    private javax.swing.JTextField txtemail;
    private javax.swing.JTextField txtendereco;
    private javax.swing.JTextField txtespecialidade;
    private javax.swing.JTextField txtnascimento;
    private javax.swing.JTextField txtnome;
    private javax.swing.JTextField txttelefone;
    // End of variables declaration//GEN-END:variables
}
